package com.ruoyi.tws.controller;

import java.util.List;
import java.io.IOException;
import java.util.Arrays;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.constant.DictConstants;
import com.ruoyi.tws.api.domain.Driver;
import lombok.RequiredArgsConstructor;
import com.ruoyi.common.security.annotation.PreAuthorize;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.core.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.Query;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ruoyi.common.core.web.page.Condition;
import com.ruoyi.tws.service.IDriverService;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 司机信息Controller
 * 
 * @author ruoyi
 * @date 2022-01-20
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/driver")
public class DriverController extends BaseController {

    private final IDriverService iDriverService;

    /**
     * 查询常用司机列表（tws_company_driver物流公司-司机关系表）
     */
//    @PreAuthorize(hasPermi="tws:driver:list")
    @GetMapping("/list")
    public TableDataInfo list(Query query, Driver driver) {
        IPage<Driver> list = iDriverService.driverList(Condition.getPage(query), driver);
        return getDataTable(list);
    }

    /**
     * 查询司机列表（tws_driver司机表）
     */
//    @PreAuthorize(hasPermi="tws:driver:list")
    @GetMapping("/listAll")
    public TableDataInfo listAll(Query query, Driver driver) {
        LambdaQueryWrapper<Driver> lqw = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(driver.getDriverName())) {
            lqw.like(Driver::getDriverName ,driver.getDriverName());
        }
        if (StringUtils.isNotBlank(driver.getDrivingLicense())) {
            lqw.eq(Driver::getDrivingLicense, driver.getDrivingLicense());
        }
        if (StringUtils.isNotBlank(driver.getTelephone())) {
            lqw.eq(Driver::getTelephone, driver.getTelephone());
        }
        if (driver.getIsCert() != null) {
            lqw.eq(Driver::getIsCert, driver.getIsCert());
        }
        if (driver.getState() != null) {
            lqw.eq(Driver::getState, driver.getState());
        }
        lqw.orderByDesc(Driver::getCreateTime);
        IPage<Driver> list = iDriverService.page(Condition.getPage(query), lqw);
        return getDataTable(list);
    }

    /**
     * 审核中计数
     */
    @GetMapping("/getCertNum")
    public AjaxResult getCertNum() {
        LambdaQueryWrapper<Driver> lqw = new LambdaQueryWrapper<>();
        lqw.eq(Driver::getIsCert, DictConstants.IS_CERT.SHZ.getKey());
        int count = iDriverService.count(lqw);
        return AjaxResult.success(count);
    }

    /**
     * 导出司机信息列表
     */
    @PreAuthorize(hasPermi = "tws:driver:export")
    @Log(title = "司机信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Driver driver) throws IOException
    {
        LambdaQueryWrapper<Driver> lqw = new LambdaQueryWrapper<Driver>(driver);
        List<Driver> list = iDriverService.list(lqw);
        ExcelUtil<Driver> util = new ExcelUtil<Driver>(Driver. class);
        util.exportExcel(response, list, "driver");
    }

    /**
     * 获取司机信息详细信息
     */
    @PreAuthorize(hasPermi="tws:driver:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(iDriverService.getById(id));
    }

    /**
     * 物流公司添加常用司机
     */
    @PreAuthorize(hasPermi="tws:driver:add")
    @Log(title = "司机信息", businessType = BusinessType.INSERT)
    @GetMapping(value = "/add/{id}")
    public AjaxResult add(@PathVariable("id") Long id) {
        return toAjax(iDriverService.add(id) ? 1 : 0);
    }

    /**
     * 修改司机信息
     */
    @PreAuthorize(hasPermi="tws:driver:edit")
    @Log(title = "司机信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Driver driver) {
        return toAjax(iDriverService.updateById(driver) ? 1 : 0);
    }

    /**
     * 司机审核
     */
    @PreAuthorize(hasPermi="tws:driver:edit")
    @Log(title = "司机信息", businessType = BusinessType.UPDATE)
    @PostMapping("/cert")
    public AjaxResult cert(@RequestBody Driver driver) {
        return toAjax(iDriverService.cert(driver));
    }

    /**
     * 司机禁/启用
     */
    @PreAuthorize(hasPermi="tws:driver:edit")
    @Log(title = "司机信息", businessType = BusinessType.UPDATE)
    @PostMapping("/stateChange")
    public AjaxResult stateChange(@RequestBody Driver driver) {
        return toAjax(iDriverService.stateChange(driver));
    }

    /**
     * 删除常用司机（tws_company_driver物流公司-司机关系表）
     */
//    @PreAuthorize(hasPermi="tws:driver:remove")
    @Log(title = "司机信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/removes/{ids}")
    public AjaxResult removes(@PathVariable Long[] ids) {
        return toAjax(iDriverService.removes(Arrays.asList(ids)) ? 1 : 0);
    }

    /**
     * 删除司机（tws_driver司机表）
     */
    @PreAuthorize(hasPermi="tws:driver:remove")
    @Log(title = "司机信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult delete(@PathVariable Long[] ids) {
        return toAjax(iDriverService.delete(Arrays.asList(ids)) ? 1 : 0);
    }

    /**
     * 上传司机
     */
    @PreAuthorize(hasPermi="tws:driver:edit")
    @Log(title = "司机信息", businessType = BusinessType.UPDATE)
    @GetMapping("/upload/{id}")
    public AjaxResult upload(@PathVariable Long id) {
        return toAjax(iDriverService.upload(id));
    }

}
